#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/log.h>
#include <libavutil/imgutils.h>

int main(int argc, char **argv){
    
    if(argc != 3){
        fprintf(stderr, "Please input input_file and output_file!\n");
    }
    char* input_file = argv[1];
    char* output_file = argv[2];
    
    printf("The input file: %s \n", input_file);
    printf("The output file: %s \n", output_file);

    int ret;
    AVFormatContext* input_ctx = NULL;
    AVFormatContext* output_ctx = NULL;
    int* stream_idx_vec = NULL;
    int num_stream = 0;
    
    ret = avformat_open_input(&input_ctx, input_file, NULL, NULL);
    if(ret < 0){
        fprintf(stderr, "avformat_open_input ERROR\n");
        goto end;
    }

    ret = avformat_find_stream_info(input_ctx, NULL);
    if(ret < 0){
        fprintf(stderr, "avformat_find_stream_info ERROR\n");
        goto end;
    }

    ret = avformat_alloc_output_context2(&output_ctx, NULL, NULL, output_file);
    if(ret < 0){
        fprintf(stderr, "avformat_alloc_output_context2 ERROR\n");
        goto end;
    }

    num_stream = input_ctx->nb_streams;
    stream_idx_vec = (int *) av_mallocz_array(num_stream, sizeof(*stream_idx_vec));
    if(stream_idx_vec == NULL){
        fprintf(stderr, "av_mallocz_array ERROR\n");
        goto end;
    }
    int stream = 0;
    for (int i=0; i<num_stream; i++){
        AVStream* in_stream = input_ctx->streams[i];
        AVStream* out_stream = NULL;
        AVCodecParameters* in_codec_par = in_stream->codecpar;
        if(in_codec_par->codec_type != AVMEDIA_TYPE_AUDIO && in_codec_par->codec_type != AVMEDIA_TYPE_VIDEO && in_codec_par->codec_type != AVMEDIA_TYPE_SUBTITLE){
            stream_idx_vec[i] = -1;
            continue;
        }
        stream_idx_vec[i] = stream++;
        out_stream = avformat_new_stream(output_ctx, NULL);
        if(out_stream == NULL){
            fprintf(stderr, "avformat_new_stream ERROR\n");
            goto end;
        }
        ret = avcodec_parameters_copy(out_stream->codecpar, in_codec_par);
        if(ret < 0){
            fprintf(stderr, "avcodec_parameters_copy ERROR\n");
            goto end;
        }
    }

    av_dump_format(output_ctx, 0, output_file, 1);
    if(!(output_ctx->oformat->flags & AVFMT_NOFILE)){
        ret = avio_open(&output_ctx->pb, output_file, AVIO_FLAG_WRITE);
        if(ret < 0){
            fprintf(stderr, "avio_open\n");
            goto end;
        }
    }

    ret = avformat_write_header(output_ctx, NULL);
    if(ret < 0){
        fprintf(stderr, "avformat_write_header ERROR\n");
        goto end;
    }

    AVPacket packet;
    while(1){
        AVStream* in_stream = NULL;
        AVStream* out_stream = NULL;
        ret = av_read_frame(input_ctx, &packet);
        if(ret < 0){
            break;
        }
        if(packet.stream_index >= num_stream || packet.stream_index < 0){
            av_packet_unref(&packet);
            continue;
        }
        in_stream = input_ctx->streams[packet.stream_index];
        packet.stream_index = stream_idx_vec[packet.stream_index];
        out_stream = output_ctx->streams[packet.stream_index];
        packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_INF | AV_ROUND_PASS_MINMAX);
        packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_INF | AV_ROUND_PASS_MINMAX);
        packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
        packet.pos = -1;

        ret = av_interleaved_write_frame(output_ctx, &packet);
        if(ret < 0){
            fprintf(stderr, "av_interleaved_write_frame ERROR\n");
            break;
        }
        av_packet_unref(&packet);
    }
    av_write_trailer(output_ctx);

end:
    avformat_close_input(&input_ctx);
    avformat_free_context(output_ctx);
    av_freep(&stream_idx_vec);
    return 0;
}